
import threading
import time


def get_cam_ready(camid):
    return 1
    
def plc_out(isok0, isok1, pos):
    return 0

def plc_out_pos(pos):
    #检测结果
    print(f'plc_out_pos {pos}')

def plc_out_ng(isok0, isok1):
    #检测结果
    PLC_OK = [ "D1020", "D1022"]
    print(f'plc_out_ng {isok0} {isok1}')

def plc_set_ng(is_ng):  # 旋轉軸速度
    print(f'plc_set_ng {is_ng}')
    return 0


def plc_work_thd(ss):
    print("进入工作状态")
    # 告知PLC CAM处于READY状态可以接受数据
    while ss.thread_working:
        ss.qq.put(1)
        time.sleep(2)
    pass

# 定时不断发送心跳信号，告知PLC
import queue

class PlcSL:
    def __init__(self) -> None:
        self.qq = queue.Queue()
        self.thread_work = None
        self.con()
    
    def con(self):
        self.start()

    def __del__(self):
        self.stop()

    def start(self, qq=None):
        if self.thread_work is None:
            self.thread_working = True
            self.thread_work = threading.Thread(target=plc_work_thd, args=(self,))
            self.thread_work.start()

    def set_pos(self, pos):
        pass

    def stop(self):
        if self.thread_work is not None:
            self.thread_working = False
            self.thread_work.join()
            self.thread_work = None

    def close(self):
        self.stop()
        pass
